package rexsee.multimedia;

import rexsee.core.browser.Browser;
import rexsee.core.browser.RexseeUrl;
import rexsee.core.browser.clazz.ActivityResult.ActivityResultListener;
import rexsee.core.browser.clazz.JavascriptInterface;
import rexsee.core.transportation.RexseeUpload;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore.Audio.Media;

public class RexseeAudioRecorder implements JavascriptInterface {

	public static final String INTERFACE_NAME = "AudioRecorder";
	@Override
	public String getInterfaceName() {
		return mBrowser.application.resources.prefix + INTERFACE_NAME;
	}
	@Override
	public JavascriptInterface getInheritInterface(Browser childBrowser) {
		return this;
	}
	@Override
	public JavascriptInterface getNewInterface(Browser childBrowser) {
		return new RexseeAudioRecorder(childBrowser);
	}

	public static final String EVENT_ONRECORDAUDIOSUCCESSED = "onRecordAudioSuccessed";
	public static final String EVENT_ONRECORDAUDIOFAILED = "onRecordAudioFailed";

	private String mediaFile = null;

	private final Browser mBrowser;

	public RexseeAudioRecorder(Browser browser) {

		mBrowser = browser;
		browser.eventList.add(EVENT_ONRECORDAUDIOSUCCESSED);
		browser.eventList.add(EVENT_ONRECORDAUDIOFAILED);

	}

	public String getLastMediaAudio() {
		ContentResolver contentResolver = mBrowser.getContext().getContentResolver();
		Cursor cursor = contentResolver.query(Uri.parse(RexseeUrl.URI_MEDIA_AUDIO), new String[]{"_data"}, null, null, "_id desc");
		if (cursor == null || cursor.getCount() == 0) return "";
		cursor.moveToFirst();
		String rtn = "file://" + cursor.getString(0);
		cursor.close();
		return rtn;
	}

	public void record() {
		try {
			Intent intent = new Intent(Media.RECORD_SOUND_ACTION);
			mBrowser.activityResult.start(intent, new ActivityResultListener() {
				@Override
				public void run(int resultCode, Intent resultIntent) {
					if (resultCode == Activity.RESULT_OK) {
						mediaFile = getLastMediaAudio();
						mBrowser.eventList.run(EVENT_ONRECORDAUDIOSUCCESSED, new String[]{mediaFile});
					} else {
						mediaFile = null;
						mBrowser.eventList.run(EVENT_ONRECORDAUDIOFAILED);
					}
				}
			});
		} catch (Exception e) {
			mBrowser.exception(getInterfaceName(), e);
		}
	}

	public void prepareUpload() {
		if (mediaFile == null) {
			mBrowser.exception(getInterfaceName(), "Audio is not ready.");
		} else {
			RexseeUpload uploadObject = (RexseeUpload) mBrowser.interfaceList.get(mBrowser.application.resources.prefix + RexseeUpload.INTERFACE_NAME);
			if (uploadObject == null) {
				mBrowser.exception(getInterfaceName(), "Upload object is not ready.");
			} else {
				uploadObject.selectedFiles.add(mediaFile);
			}
		}
	}

	public void upload(String action, String name) {
		if (mediaFile == null) {
			mBrowser.exception(getInterfaceName(), "Audio is not ready.");
		} else {
			RexseeUpload uploadObject = (RexseeUpload) mBrowser.interfaceList.get(mBrowser.application.resources.prefix + RexseeUpload.INTERFACE_NAME);
			if (uploadObject == null) {
				mBrowser.exception(getInterfaceName(), "Upload object is not ready.");
			} else {
				uploadObject.clear();
				uploadObject.selectedFiles.add(mediaFile);
				uploadObject.upload(action, name);
			}
		}
	}

}
